using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pampa.Common.Pages;
using Pampa.WebControls;

namespace Pampa.WebControls
{
    [ToolboxData("<{0}:CollapsablePanel runat=server></{0}:CollapsablePanel>")]
    [DesignerAttribute(typeof(System.Web.UI.Design.ContainerControlDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class CollapsablePanel : Panel
    {
        private string title;
        private bool titleBar;
        private bool expanded;
        private bool collapsableButton;

        #region Constructors

        public CollapsablePanel()
        {
            this.TitleBar = true;
            this.Expanded = true;
            this.CollapsableButton = true;
            this.CssClass = "collapsablePanelDefault";
        }

        public CollapsablePanel(string id) : this()
        {
            this.ID = id;
        }

        #endregion

        #region Properties

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public bool TitleBar
        {
            get { return titleBar; }
            set { titleBar = value; }
        }

        public bool CollapsableButton
        {
            get { return collapsableButton; }
            set { collapsableButton = value; }
        }

        public bool Expanded
        {
            get { return expanded; }
            set { expanded = value; }
        }

        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.CssClass == "collapsablePanelDefault")
                AbstractPage.RegisterStyleSheet(this.Page, "Pampa.WebControls/" + GetDesignModeState() + "CollapsablePanel.css");

            AbstractPage.RegisterLibrary(this.Page, "Pampa.WebControls/CollapsablePanel.js");
        }

        #endregion

        #region Render

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if(this.TitleBar)
                RenderTitle(writer);

            RenderPanel(writer);
        }

        protected virtual void RenderTitle(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "title");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            // Titulo
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(this.Title);
            writer.RenderEndTag();

            // Boton expande/colapsa
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            writer.AddStyleAttribute("padding-right", "5px");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            if (this.CollapsableButton)
            {
                string collapsableMethod = "CollapsablePanel.collapseExpand(this, \"" + this.ID + "\")";
                ClientButton button = new ClientButton(this.ID + "_button", ButtonType.Button, collapsableMethod);
                button.CssClass = "buttonUp";
                button.RollOver = false;
                button.Focuseable = false;
                button.RenderControl(writer);
            }

            writer.RenderEndTag();

            writer.RenderEndTag(); // Tr
            writer.RenderEndTag(); // Table
        }

        protected virtual void RenderPanel(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "container");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID + "_container");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, (this.Expanded ? "" : "none"));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            // Renderea todos los controles contenidos en el panel
            base.RenderContents(writer);

            writer.RenderEndTag();
        }

        #endregion
    }
}
